/**
 *  TextMash - simple IDE for Clojure
 *  
 *  Copyright (C) 2010 Aleksander Naszko
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package pl.olek.textmash.event;

import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeMap;

/**
 * 
 * @author anaszko
 *
 */
public class EventDispatcher {

	private static EventDispatcher singleton;

	public synchronized static EventDispatcher getInstance() {
		if (singleton == null) {
			singleton = new EventDispatcher();
		}
		return singleton;
	}

	public static final String WORKSPACE_IS_DIRTY = "WORKSPACE_IS_DIRTY";
	public static final String WORKSPACE_SAVED = "WORKSPACE_SAVED";
	public static final String REMOTE_CHANGED = "REMOTE_CHANGED";
	
	HashMap<Object, TreeMap<String, HashSet<EventListener>>> registry = new HashMap<Object, TreeMap<String, HashSet<EventListener>>>();

	public synchronized boolean fire(Object group, String name) {
//		 System.out.println("Fired " + name + " for " + (group != null ? 
//				 group.hashCode() : 0 ));
		TreeMap<String, HashSet<EventListener>> listeners = registry.get(group);
		if (listeners == null) {
			if (group != null) {
				return fire(null, name);
			}
			return true;
		}
		HashSet<EventListener> events = listeners.get(name);
		if (events == null) {
			if (group != null) {
				return fire(null, name);
			}
			return true;
		}
		Event event = new Event(name);
		for (EventListener e : events) {
			e.action(event);
			// System.out.println("Called " + name + " for " + e.hashCode());
			if (event.isAborted()) {
				return false;
			}
		}
		if (group != null) {
			return fire(null, name);
		}
		return true;
	}

	public synchronized void register(Object group, EventListener listener,
			String... names) {
		for (String name : names) {
			register(group, name, listener);
		}

	}

	public synchronized void register(EventListener listener, String... names) {
		for (String name : names) {
			register(null, name, listener);
		}

	}

	public synchronized void register(Object group, String name,
			EventListener listener) {
		TreeMap<String, HashSet<EventListener>> listeners = registry.get(group);
		if (listeners == null) {
			listeners = new TreeMap<String, HashSet<EventListener>>();
			registry.put(group, listeners);
		}
		HashSet<EventListener> events = listeners.get(name);
		if (events == null) {
			events = new HashSet<EventListener>();
			listeners.put(name, events);
		}
		events.add(listener);
	}

	public synchronized void unregister(EventListener listener) {
		for (TreeMap<String, HashSet<EventListener>> entry : registry.values()) {
			for (HashSet<EventListener> events : entry.values()) {
				events.remove(listener);
			}
		}
	}
}
